package callnumber.gtdev5.com.photoVault.widget;

import android.animation.AnimatorSet;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.bean.Gds;
import callnumber.gtdev5.com.photoVault.constants.AppConstans;
import callnumber.gtdev5.com.photoVault.customview.MyOnItemClickListener;
import callnumber.gtdev5.com.photoVault.holder.DialogMemberHolder;
import callnumber.gtdev5.com.photoVault.holder.SpacesItemDecoration;


/**
 * Created by cheng
 * PackageName APP_Lock
 * 2018/1/31 11:44
 */

public class DialogShowMember extends BaseDialog {
    private RecyclerView recyclerView;
    private TextView txt_sure;
    private ImageView imageView;
    private DialogMemberAdapter adapter;
    private Context mContext;
    private List<Gds> mList;
    private TextView tv_dialog_tip;
    private onClickListener onClickListener;

    public void setBackPress(OnBackPress backPress) {
        this.backPress = backPress;
    }

    private OnBackPress backPress;

    public DialogShowMember(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected float setWidthScale() {
        return 0.8f;
    }

    @Override
    protected AnimatorSet setEnterAnim() {
        return null;
    }

    @Override
    protected AnimatorSet setExitAnim() {
        return null;
    }

    @Override
    protected void init() {
        mList = new ArrayList<>();
        recyclerView = findViewById(R.id.newmember_recycleview);
        txt_sure = findViewById(R.id.txt_sure);
        imageView = findViewById(R.id.newmember_close);
        if (DataSupport.findAll(Gds.class) != null) {
            mList = DataSupport.findAll(Gds.class);
            GridLayoutManager manager = new GridLayoutManager(mContext, 1);
            recyclerView.setLayoutManager(manager);
            recyclerView.addItemDecoration(new SpacesItemDecoration(20));
        }
        tv_dialog_tip = findViewById(R.id.vip_dialog_text);
        adapter = new DialogMemberAdapter(mContext, mList);
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);
//        adapter.setOnItemClickListener((view, position) -> {
//            Gds gds = mList.get(position);
//            for (Gds bean :
//                    mList) {
//                bean.setSelect(false);
//            }
//            gds.setSelect(true);
//            adapter.notifyDataSetChanged();
//        });

//        txt_sure.setOnClickListener(v -> {
//            if (onClickListener != null) {
//                dismiss();
//                onClickListener.onClick(true, adapter.getGid());
//            }
//        });
        imageView.setOnClickListener(v -> {
            if (onClickListener != null) {
                dismiss();
                onClickListener.onClick(false, 0);
            }
        });


    }

    @Override
    public void onBackPressed() {
        if (backPress != null) {
            backPress.OnBackPress();
        }
        super.onBackPressed();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_showmember;
    }

    public void setOnClickListener(onClickListener listener) {
        this.onClickListener = listener;
    }

    public interface onClickListener {
        void onClick(boolean isClicked, int id);
    }

    public interface OnBackPress {
        void OnBackPress();
    }

    public void setText(String s) {
        if (tv_dialog_tip != null) {
            tv_dialog_tip.setText(s);
        }
    }

    public void initNotipCheckbox() {
        findViewById(R.id.ll_notip_again).setVisibility(View.VISIBLE);
        CheckBox checkBox = findViewById(R.id.cb_notipagain);
        TextView textView = findViewById(R.id.tv_notipagain);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SpUtils.getInstance().putBoolean(AppConstans.NOVIP_STILLTIP, false);
                    textView.setTextColor(mContext.getResources().getColor(R.color.check_text_true));
                } else {
                    SpUtils.getInstance().putBoolean(AppConstans.NOVIP_STILLTIP, true);
                    textView.setTextColor(mContext.getResources().getColor(R.color.check_text_faule));
                }
            }
        });
    }

    public class DialogMemberAdapter extends RecyclerView.Adapter implements View.OnClickListener {

        private Context mContext;
        private LayoutInflater mInflater;
        private List<Gds> mList = new ArrayList<>();
        private int gid;
        private MyOnItemClickListener onItemClickListener;
        private int currentPosition;

        private int getcurrentPosition() {
            return currentPosition;
        }

        public DialogMemberAdapter(Context mContext, List<Gds> list) {
            this.mContext = mContext;
            this.mList = list;
            this.mInflater = LayoutInflater.from(mContext);
            currentPosition = mList.size() - 1;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = null;
            DialogMemberHolder holder = null;
            if (view == null) {
                view = mInflater.inflate(R.layout.item_newmemer, parent, false);
                holder = new DialogMemberHolder(view);
                view.setTag(holder);
            } else {
                holder = (DialogMemberHolder) view.getTag();
            }
            if (view != null) {
                view.setOnClickListener(this);
            }

            return holder;
        }

        int i;

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof DialogMemberHolder) {
                Gds gds = mList.get(position);
                DialogMemberHolder dialogMemberHolder = (DialogMemberHolder) holder;
                dialogMemberHolder.tv_time.setText(gds.getName());
                dialogMemberHolder.tv_price.setText(gds.getPrice());
//                if (position == currentPosition) {
////                dialogMemberHolder.linearLayout.setBackgroundResource(R.drawable.shape_vipdialog_gds_bg_red);
//                    dialogMemberHolder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.member_click_text));
//                    dialogMemberHolder.tv_price.setTextColor(ContextCompat.getColor(mContext, R.color.member_click_text));
//                    gid = gds.getGid();
//                } else {
////                dialogMemberHolder.linearLayout.setBackgroundResource(R.drawable.shape_vipdialog_gds_bg_gre);
//                    dialogMemberHolder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.member_item_text));
//                    dialogMemberHolder.tv_price.setTextColor(ContextCompat.getColor(mContext, R.color.member_item_text));
//                }
//                dialogMemberHolder.itemView.setTag(position);
//                dialogMemberHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        currentPosition = position;
//                        notifyDataSetChanged();
//                    }
//                });

                dialogMemberHolder.txt_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickListener.onClick(true, gds.getGid());
                    }
                });
            }
        }

        public int getGid() {
            return gid;
        }

        @Override
        public int getItemCount() {
            return mList.size() > 0 ? mList.size() : 0;
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemcLick(v, (Integer) v.getTag());
            }
        }

        public void setOnItemClickListener(MyOnItemClickListener listener) {
            this.onItemClickListener = listener;
        }
    }


}
